<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/31
 * Time: 10:02
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Information  extends Base {

    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.information_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($information_selected)&&$information_selected!="")
            {
                $map['a.information_selected'] = $information_selected;
            }
            if(isset($information_type)&&$information_type!="")
            {
                $map['a.information_type'] = $information_type;
            }
            $map['is_publish'] = 1;
            $count = Db::table('up_information')->alias('a')
                ->join('up_interest uit','uit.interest_id = a.information_type_id','left')
                ->join('up_admin ua','ua.id = a.information_zh_user','left')
                ->where($map)
                ->count();
            $list = Db::table('up_information')->alias('a')
                ->join('up_interest uit','uit.interest_id = a.information_type_id','left')
                ->join('up_admin ua','ua.id = a.information_zh_user','left')
                ->where($map)
                ->field('a.*,uit.interest_word,ua.username')
                ->order('a.information_id desc')
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){
                if($vo['is_admin']==1){//前台用户发布
                    $user_username1 = Db::table('up_u_user')->where('user_id',$vo['information_user_id'])->value('user_username');
                }elseif ($vo['is_admin']==2){//后台发布
                    $user_username1 = Db::table('up_admin')->where('id',$vo['information_user_id'])->value('username');
                }
                $vo['user_username1'] = $user_username1;
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $info  = Db::table('up_interest')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    //添加
    public function add(){
        if(request()->isPost()){
           if(empty(input('param.information_type'))){
               return json(['code' => 100, 'msg' => '请选择分类']);
           }
           $data['information_title'] = input('param.information_title');
           $data['information_type_id'] = input('param.information_type');
           $data['information_content'] = input('param.content');
           $data['is_admin'] = 2;
           $data['information_url'] = input('param.photo');
           $data['information_week'] = date('Y-m-d',time());
           $data['information_time'] = date('Y-m-d H:i:s',time());
           $data['information_fa_time'] = date('Y-m-d H:i:s',time());
           $data['information_zh_user'] = session('uid');
           $data['information_user_id'] = session('uid');
           $data['information_thumb'] = explode(',',$data['information_url'])[0];
            $res = Db::table('up_information')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $info  = Db::table('up_interest')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }

    //上架下架
    public function upAndDown(){
        $id  = input('param.id');
        $info = Db::table('up_information')->where('information_id',$id)->find();
        if($info['is_show']==1){
            $is_show = 2;
        }elseif ($info['is_show']==2){
            $is_show = 1;
        }
        $res= Db::table('up_information')->where('information_id',$id)->setField('is_show',$is_show);
        Db::table('up_information')->where('information_id',$id)->setField('information_zh_user',session('uid'));
        if($res == 1){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //精选取消精选
    public function Selected(){
        $id  = input('param.id');
        $info = Db::table('up_information')->where('information_id',$id)->find();
        if($info['information_selected']==1){
            $is_show = 2;
        }elseif ($info['information_selected']==2){
            $is_show = 1;
        }
        $res= Db::table('up_information')->where('information_id',$id)->setField('information_selected',$is_show);
        if($res ){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //删除
    public function del(){
        $id  = input('param.id');
        $where['type'] = 1;
        $where['f_id'] = $id;
        $info = Db::table('up_comment')->where($where)->count();
        $info1 = Db::table('up_comment')->where($where)->select();
        if($info>0){
            foreach ($info1 as $key=>&$vo){
                $where1['f_id'] = $vo['comment_id'];
                $count1 = Db::table('up_comment_reply')->where($where1)->count();
                if($count1>0){
                    Db::table('up_comment_reply')->where($where1)->delete();
                }
            }
           Db::table('up_comment')->where($where)->delete();

        }
        $res = Db::table('up_information')->where('information_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    //编辑
    public function edit(){
        if(request()->isPost()){
            $id = input('param.id');
            $data['information_title'] = input('param.information_title');
            $data['information_type_id'] = input('param.information_type');
            $data['information_content'] = input('param.content');
            $data['information_url'] = input('param.photo');
            $data['information_time'] = date('Y-m-d H:i:s',time());
            $data['information_zh_user'] = session('uid');
            $data['information_thumb'] = explode(',',$data['information_url'])[0];
            $res = Db::table('up_information')->where('information_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $info1  =  Db::table('up_interest')->select();
        $this->assign('info1',$info1);
        $id = input('param.id');
        $info = Db::table('up_information')->where('information_id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    //详情
    /**
     * @return array
     */
    public function detail()
    {
        $id = input('param.id');
        $list = Db::table('up_information')->alias('a')
            ->join('up_admin ua','ua.id = a.information_zh_user','left')
            ->where('information_id',$id)
            ->field('a.*,ua.username')
            ->find();
        if($list['is_admin']==1){//前台用户发布
            $user_username1 = Db::table('up_u_user')->where('user_id',$list['information_user_id'])->value('user_username');
        }elseif ($list['is_admin']==2){//后台发布
            $user_username1 = Db::table('up_admin')->where('id',$list['information_user_id'])->value('username');
        }
        $list['user_username1'] = $user_username1;
        $this->assign('list',$list);
        return $this->fetch();
    }
}